#!/bin/sh

install_file()
{
	src="$1"
	dest="$2"

	if [ ! -e "$dest" ]
	then
		cp "$src" "${dest}"
		return 1
	else
		echo "Service file already exists, skip creation"
		return 0
	fi
}


echo "Finalizing HyperHDR setup"

#check system
CPU_RPI=`grep -m1 -c 'BCM2708\|BCM2709\|BCM2710\|BCM2835\|BCM2836\|BCM2837\|BCM2711' /proc/cpuinfo`
CPU_X32X64=`uname -m | grep 'x86_32\|i686\|x86_64' | wc -l`

#Check for a bootloader as Berryboot
BOOT_BERRYBOOT=$(grep -m1 -c '\(/var/media\|/media/pi\)/berryboot' /etc/mtab)

#get current system ip
NET_IP=`hostname -I | cut -d " " -f1`

# search for users in system, returns first entry
FOUND_USR=`who | grep -o -m1 '^\w*\b'` || "root"

# determine if we should use a service
ENABLE_XSYS=0
ENABLE_SERVICE=0
STARTUP_MSG="Service is NOT enabled by default for GUI or non-systemd OS"

if [ $CPU_RPI -eq 1 ]; then
	ENABLE_SERVICE=1
	STARTUP_MSG="HyperHDR is installed as a service and starts automatically"
fi

start_msg=""
restart_msg=""

if grep -m1 systemd /proc/1/comm > /dev/null
then	
	# systemd
	if [ -z "${DISPLAY}" ]; then
		echo "HyperHDR is installed as a service and starts automatically"
		ENABLE_SERVICE=1
	else
		echo "Init daemon: GUI detected (service is NOT enabled by default, you can run HyperHDR as an application or service with 'systemctl enable (and next: start) hyperhdr@${FOUND_USR}.service')"
		ENABLE_SERVICE=0
		ENABLE_XSYS=1
	fi
	
	install_file /usr/share/hyperhdr/service/hyperhdr.systemd /etc/systemd/system/hyperhdr@.service	

	if [ $ENABLE_SERVICE -eq 1 ]; then
		systemctl enable hyperhdr"@${FOUND_USR}".service
		start_msg="systemctl start hyperhdr for user ${FOUND_USR}"
		systemctl start hyperhdr"@${FOUND_USR}"
	fi

elif [ -e /sbin/initctl ]
then	
	# upstart
	if [ $ENABLE_SERVICE -eq 1 ]; then
		echo "Init daemon: upstart (service is enabled by default)"
		install_file /usr/share/hyperhdr/service/hyperhdr.initctl /etc/init/hyperhdr.conf && initctl reload-configuration
		start_msg="initctl start hyperhdr"
		initctl start hyperhdr
	else
		echo "Init daemon: upstart (service is NOT enabled by default)"
	fi

else	
	# sysV
	if [ $ENABLE_SERVICE -eq 1 ]; then
		echo "Init daemon: sysV (enabled by default)"
		install_file /usr/share/hyperhdr/service/hyperhdr.init /etc/init.d/hyperhdr && chmod +x /etc/init.d/hyperhdr && update-rc.d hyperhdr defaults 98 02
		start_msg="service hyperhdr start"
		service hyperhdr start
	else
		echo "Init daemon: sysV (service is NOT enabled by default)"
	fi
fi

#cleanup
rm -r /usr/share/hyperhdr/service

#link binarys and set exec bit
BINSP=/usr/share/hyperhdr/bin
BINTP=/usr/bin
chmod +x -R $BINSP
ln -fs $BINSP/hyperhdr $BINTP/hyperhdr

# install desktop icons
echo "Install HyperHDR desktop icons"
mkdir /usr/share/pixmaps/hyperhdr 2>/dev/null
cp /usr/share/hyperhdr/desktop/*.png /usr/share/pixmaps/hyperhdr 2>/dev/null
desktop-file-install /usr/share/hyperhdr/desktop/hyperhdr.desktop 2>/dev/null

# cleanup desktop icons
rm -r /usr/share/hyperhdr/desktop 2>/dev/null

#Check, if dtparam=spi=on is in place
if [ $CPU_RPI -eq 1 ]; then
	BOOT_DIR="/boot"
	if [ $BOOT_BERRYBOOT -eq 1 ]; then
		BOOT_DIR=$(sed -ne "s#/dev/mmcblk0p1 \([^ ]*\) vfat rw,.*#\1#p" /etc/mtab)
	fi
	if [ -z "$BOOT_DIR" -o ! -f "$BOOT_DIR/config.txt" ]; then
		echo 'Warning: RPi using BERRYBOOT found but can not locate where config.txt is to enable SPI. (BOOT_DIR='"$BOOT_DIR)"
		SPIOK=1 # Not sure if OK, but don't ask to reboot
	else
		SPIOK=`grep '^\dtparam=spi=on' "$BOOT_DIR/config.txt" | wc -l`
		if [ $SPIOK -ne 1 ]; then
			echo 'Raspberry Pi found, but SPI is not set, we write "dtparam=spi=on" to '"$BOOT_DIR/config.txt"
			sed -i '$a dtparam=spi=on' "$BOOT_DIR/config.txt"
			REBOOTMESSAGE="Restart Raspberry Pi, SPI has been enabled in $BOOT_DIR/config.txt"
		fi
	fi
fi

if command -v firewall-cmd &> /dev/null
then
	echo "Firewall: opening port 8090"
	firewall-cmd --add-port=8090/tcp
fi

echo ${start_msg}

printf "\n +-----------------------------------------------------------------------+"
printf "\n |                 \033[32;1mHyperHDR has been installed/updated!\033[0m                  |"
printf "\n +-----------------------------------------------------------------------+"
printf "\n |  For configuration, visit with your browser: \033[37;1m%13s:%s\033[0m       |" "$NET_IP" "8090"
printf "\n |  If already used by another service try: \033[37;1m%13s:%s\033[0m           |" "$NET_IP" "8091"
printf "\n |  Start the service: \033[37;1msudo systemctl start hyperhdr@%-12s\033[0m        |" "$FOUND_USR"
printf "\n |  Stop the service: \033[37;1msudo systemctl stop hyperhdr@%-12s\033[0m          |" "$FOUND_USR"
printf "\n |  Troubleshooting? Run HyperHDR manually: \033[37;1m/usr/bin/hyperhdr\033[0m            |"
printf "\n +-----------------------------------------------------------------------+"
case "$STARTUP_MSG" in 
  *"HyperHDR is installed as a service"*)
	printf "\n |  \033[32;1m%-67s\033[0m  |" "$STARTUP_MSG";;
  *) printf "\n |  \033[31;1m%-67s\033[0m  |" "$STARTUP_MSG"
esac
[ -z "$REBOOTMESSAGE" ] || printf "\n |  \033[31;1m%-67s\033[0m  |" "$REBOOTMESSAGE"
printf "\n +-----------------------------------------------------------------------+"
printf "\n |  Webpage: \033[36;1mhttps://hyperhdr.eu\033[0m                                         |"
printf "\n |  GitHub: \033[36;1mhttps://github.com/awawa-dev/HyperHDR\033[0m                        |"
printf "\n +-----------------------------------------------------------------------+\n"

exit 0
